home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrtools-1.10 / libhfs_iso / internal.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-06  |  12.3 KB  |  357 lines

  1. /*
  2.  * hfsutils - tools for reading and writing Macintosh HFS volumes
  3.  * Copyright (C) 1996, 1997 Robert Leslie
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. # include <time.h>
  21. # include <standard.h>
  22.  
  23. # include "hfs.h"
  24.  
  25. # define ERROR(code, str)    (hfs_error = (str), errno = (code))
  26.  
  27. # define SIZE(type, n)        ((size_t) (sizeof(type) * (n)))
  28. # define ALLOC(type, n)        ((type *) malloc(SIZE(type, n)))
  29. # define ALLOCX(type, n)    ((n) ? ALLOC(type, n) : (type *) 0)
  30. # define FREE(ptr)        {if (ptr) free((void *) ptr) ;}
  31.  
  32. # define REALLOC(ptr, type, n)  \
  33.     ((type *) ((ptr) ? realloc(ptr, SIZE(type, n)) : malloc(SIZE(type, n))))
  34. # define REALLOCX(ptr, type, n)  \
  35.     ((n) ? REALLOC(type, n) : (FREE(ptr), (type *) 0))
  36.  
  37. # define BMTST(bm, num)  \
  38.   (((char *) (bm))[(num) >> 3] & (0x80 >> ((num) & 0x07)))
  39. # define BMSET(bm, num)  \
  40.   (((char *) (bm))[(num) >> 3] |= (0x80 >> ((num) & 0x07)))
  41. # define BMCLR(bm, num)  \
  42.   (((char *) (bm))[(num) >> 3] &= ~(0x80 >> ((num) & 0x07)))
  43.  
  44. typedef unsigned char block[HFS_BLOCKSZ];
  45.  
  46. typedef signed char    Char;
  47. typedef unsigned char    UChar;
  48. typedef signed char    SignedByte;
  49. typedef signed short    Integer;
  50. typedef unsigned short    UInteger;
  51. typedef signed long    LongInt;
  52. typedef unsigned long    ULongInt;
  53. typedef char        Str15[16];
  54. typedef char        Str31[32];
  55. typedef long        OSType;
  56.  
  57. typedef struct {
  58.   UInteger    xdrStABN;    /* first allocation block */
  59.   UInteger    xdrNumABlks;    /* number of allocation blocks */
  60. } ExtDescriptor;
  61.  
  62. typedef ExtDescriptor ExtDataRec[3];
  63.  
  64. typedef struct {
  65.   SignedByte    xkrKeyLen;    /* key length */
  66.   SignedByte    xkrFkType;    /* fork type (0x00/0xff == data/resource */
  67.   ULongInt    xkrFNum;    /* file number */
  68.   UInteger    xkrFABN;    /* starting file allocation block */
  69. } ExtKeyRec;
  70.  
  71. typedef struct {
  72.   SignedByte    ckrKeyLen;    /* key length */
  73.   SignedByte    ckrResrv1;    /* reserved */
  74.   ULongInt    ckrParID;    /* parent directory ID */
  75.   Str31        ckrCName;    /* catalog node name */
  76. } CatKeyRec;
  77.  
  78. # define HFS_MAP1SZ  256
  79. # define HFS_MAPXSZ  492
  80.  
  81. # define HFS_NODEREC(nd, rnum)    ((nd).data + (nd).roff[rnum])
  82.  
  83. # define HFS_RECKEYLEN(ptr)    (*(unsigned char *) (ptr))
  84. # define HFS_RECKEYSKIP(ptr)    ((1 + HFS_RECKEYLEN(ptr) + 1) & ~1)
  85. # define HFS_RECDATA(ptr)    ((ptr) + HFS_RECKEYSKIP(ptr))
  86.  
  87. # define HFS_CATDATALEN        sizeof(CatDataRec)
  88. # define HFS_EXTDATALEN        sizeof(ExtDataRec)
  89.  
  90. # define HFS_CATKEYLEN        sizeof(CatKeyRec)
  91. # define HFS_EXTKEYLEN        sizeof(ExtKeyRec)
  92.  
  93. # define HFS_CATRECMAXLEN    (HFS_CATKEYLEN + HFS_CATDATALEN)
  94. # define HFS_EXTRECMAXLEN    (HFS_EXTKEYLEN + HFS_EXTDATALEN)
  95.  
  96. # define HFS_MAXRECLEN        HFS_CATRECMAXLEN
  97.  
  98. typedef struct {
  99.   Integer    v;        /* vertical coordinate */
  100.   Integer    h;        /* horizontal coordinate */
  101. } Point;
  102.  
  103. typedef struct {
  104.   Integer    top;        /* top edge of rectangle */
  105.   Integer    left;        /* left edge */
  106.   Integer    bottom;        /* bottom edge */
  107.   Integer    right;        /* rightmost edge */
  108. } Rect;
  109.  
  110. typedef struct {
  111.   Rect        frRect;        /* folder's rectangle */
  112.   Integer    frFlags;    /* flags */
  113.   Point        frLocation;    /* folder's location */
  114.   Integer    frView;        /* folder's view */
  115. } DInfo;
  116.  
  117. typedef struct {
  118.   Point        frScroll;    /* scroll position */
  119.   LongInt    frOpenChain;    /* directory ID chain of open folders */
  120.   Integer    frUnused;    /* reserved */
  121.   Integer    frComment;    /* comment ID */
  122.   LongInt    frPutAway;    /* directory ID */
  123. } DXInfo;
  124.  
  125. typedef struct {
  126.   OSType    fdType;        /* file type */
  127.   OSType    fdCreator;    /* file's creator */
  128.   Integer    fdFlags;    /* flags */
  129.   Point        fdLocation;    /* file's location */
  130.   Integer    fdFldr;        /* file's window */
  131. } FInfo;
  132.  
  133. typedef struct {
  134.   Integer    fdIconID;    /* icon ID */
  135.   Integer    fdUnused[4];    /* reserved */
  136.   Integer    fdComment;    /* comment ID */
  137.   LongInt    fdPutAway;    /* home directory ID */
  138. } FXInfo;
  139.  
  140. typedef struct {
  141.   Integer    drSigWord;    /* volume signature (0x4244 for HFS) */
  142.   LongInt    drCrDate;    /* date and time of volume creation */
  143.   LongInt    drLsMod;    /* date and time of last modification */
  144.   Integer    drAtrb;        /* volume attributes */
  145.   UInteger    drNmFls;    /* number of files in root directory */
  146.   UInteger    drVBMSt;    /* first block of volume bit map (always 3) */
  147.   UInteger    drAllocPtr;    /* start of next allocation search */
  148.   UInteger    drNmAlBlks;    /* number of allocation blocks in volume */
  149.   ULongInt    drAlBlkSiz;    /* size (in bytes) of allocation blocks */
  150.   ULongInt    drClpSiz;    /* default clump size */
  151.   UInteger    drAlBlSt;    /* first allocation block in volume */
  152.   LongInt    drNxtCNID;    /* next unused catalog node ID (dir/file ID) */
  153.   UInteger    drFreeBks;    /* number of unused allocation blocks */
  154.   char        drVN[28];    /* volume name (1-27 chars) */
  155.   LongInt    drVolBkUp;    /* date and time of last backup */
  156.   Integer    drVSeqNum;    /* volume backup sequence number */
  157.   ULongInt    drWrCnt;    /* volume write count */
  158.   ULongInt    drXTClpSiz;    /* clump size for extents overflow file */
  159.   ULongInt    drCTClpSiz;    /* clump size for catalog file */
  160.   UInteger    drNmRtDirs;    /* number of directories in root directory */
  161.   ULongInt    drFilCnt;    /* number of files in volume */
  162.   ULongInt    drDirCnt;    /* number of directories in volume */
  163.   LongInt    drFndrInfo[8];    /* information used by the Finder */
  164.   UInteger    drVCSize;    /* size (in blocks) of volume cache */
  165.   UInteger    drVBMCSize;    /* size (in blocks) of volume bitmap cache */
  166.   UInteger    drCtlCSize;    /* size (in blocks) of common volume cache */
  167.   ULongInt    drXTFlSize;    /* size (in bytes) of extents overflow file */
  168.   ExtDataRec    drXTExtRec;    /* first extent record for extents file */
  169.   ULongInt    drCTFlSize;    /* size (in bytes) of catalog file */
  170.   ExtDataRec    drCTExtRec;    /* first extent record for catalog file */
  171. } MDB;
  172.  
  173. # define HFS_ATRB_BUSY        (1 <<  6)
  174. # define HFS_ATRB_HLOCKED    (1 <<  7)
  175. # define HFS_ATRB_UMOUNTED    (1 <<  8)
  176. # define HFS_ATRB_BBSPARED    (1 <<  9)
  177. # define HFS_ATRB_COPYPROT    (1 << 14)
  178. # define HFS_ATRB_SLOCKED    (1 << 15)
  179.  
  180. typedef enum {
  181.   cdrDirRec  = 1,
  182.   cdrFilRec  = 2,
  183.   cdrThdRec  = 3,
  184.   cdrFThdRec = 4
  185. } CatDataType;
  186.  
  187. typedef struct {
  188.   SignedByte    cdrType;    /* record type */
  189.   SignedByte    cdrResrv2;    /* reserved */
  190.   union {
  191.     struct {  /* cdrDirRec */
  192.       Integer    dirFlags;    /* directory flags */
  193.       UInteger    dirVal;        /* directory valence */
  194.       ULongInt    dirDirID;    /* directory ID */
  195.       LongInt    dirCrDat;    /* date and time of creation */
  196.       LongInt    dirMdDat;    /* date and time of last modification */
  197.       LongInt    dirBkDat;    /* date and time of last backup */
  198.       DInfo    dirUsrInfo;    /* Finder information */
  199.       DXInfo    dirFndrInfo;    /* additional Finder information */
  200.       LongInt    dirResrv[4];    /* reserved */
  201.     } dir;
  202.     struct {  /* cdrFilRec */
  203.       SignedByte
  204.         filFlags;    /* file flags */
  205.       SignedByte
  206.         filTyp;        /* file type */
  207.       FInfo    filUsrWds;    /* Finder information */
  208.       ULongInt    filFlNum;    /* file ID */
  209.       UInteger    filStBlk;    /* first alloc block of data fork */
  210.       ULongInt    filLgLen;    /* logical EOF of data fork */
  211.       ULongInt    filPyLen;    /* physical EOF of data fork */
  212.       UInteger    filRStBlk;    /* first alloc block of resource fork */
  213.       ULongInt    filRLgLen;    /* logical EOF of resource fork */
  214.       ULongInt    filRPyLen;    /* physical EOF of resource fork */
  215.       LongInt    filCrDat;    /* date and time of creation */
  216.       LongInt    filMdDat;    /* date and time of last modification */
  217.       LongInt    filBkDat;    /* date and time of last backup */
  218.       FXInfo    filFndrInfo;    /* additional Finder information */
  219.       UInteger    filClpSize;    /* file clump size */
  220.       ExtDataRec
  221.         filExtRec;    /* first data fork extent record */
  222.       ExtDataRec
  223.         filRExtRec;    /* first resource fork extent record */
  224.       LongInt    filResrv;    /* reserved */
  225.     } fil;
  226.     struct {  /* cdrThdRec */
  227.       LongInt    thdResrv[2];    /* reserved */
  228.       ULongInt    thdParID;    /* parent ID for this directory */
  229.       Str31    thdCName;    /* name of this directory */
  230.     } dthd;
  231.     struct {  /* cdrFThdRec */
  232.       LongInt    fthdResrv[2];    /* reserved */
  233.       ULongInt    fthdParID;    /* parent ID for this file */
  234.       Str31    fthdCName;    /* name of this file */
  235.     } fthd;
  236.   } u;
  237. } CatDataRec;
  238.  
  239. struct _hfsfile_ {
  240.   struct _hfsvol_ *vol;        /* pointer to volume descriptor */
  241.   long parid;            /* parent directory ID of this file */
  242.   char name[HFS_MAX_FLEN + 1];    /* catalog name of this file */
  243.   CatDataRec cat;        /* catalog information */
  244.   ExtDataRec ext;        /* current extent record */
  245.   unsigned int fabn;        /* starting file allocation block number */
  246.   int fork;            /* current selected fork for I/O */
  247.   unsigned long pos;        /* current file seek pointer */
  248.   unsigned long clump;        /* file's clump size, for allocation */
  249.   int flags;            /* bit flags */
  250.  
  251.   struct _hfsfile_ *prev;
  252.   struct _hfsfile_ *next;
  253. };
  254.  
  255. # define HFS_UPDATE_CATREC    0x01
  256.  
  257. typedef struct {
  258.   ULongInt    ndFLink;    /* forward link */
  259.   ULongInt    ndBLink;    /* backward link */
  260.   SignedByte    ndType;        /* node type */
  261.   SignedByte    ndNHeight;    /* node level */
  262.   UInteger    ndNRecs;    /* number of records in node */
  263.   Integer    ndResv2;    /* reserved */
  264. } NodeDescriptor;
  265.  
  266. # define HFS_MAXRECS    35    /* maximum based on minimum record size */
  267.  
  268. typedef struct _node_ {
  269.   struct _btree_ *bt;        /* btree to which this node belongs */
  270.   unsigned long nnum;        /* node index */
  271.   NodeDescriptor nd;        /* node descriptor */
  272.   int rnum;            /* current record index */
  273.   UInteger roff[HFS_MAXRECS + 1];    /* record offsets */
  274.   block data;            /* raw contents of node */
  275. } node;
  276.  
  277. enum {
  278.   ndIndxNode = 0x00,
  279.   ndHdrNode  = 0x01,
  280.   ndMapNode  = 0x02,
  281.   ndLeafNode = 0xff
  282. };
  283.  
  284. struct _hfsdir_ {
  285.   struct _hfsvol_ *vol;        /* associated volume */
  286.   long dirid;            /* directory ID of interest (or 0) */
  287.  
  288.   node n;            /* current B*-tree node */
  289.   struct _hfsvol_ *vptr;    /* current volume pointer */
  290.  
  291.   struct _hfsdir_ *prev;
  292.   struct _hfsdir_ *next;
  293. };
  294.  
  295. typedef struct {
  296.   UInteger    bthDepth;    /* current depth of tree */
  297.   ULongInt    bthRoot;    /* number of root node */
  298.   ULongInt    bthNRecs;    /* number of leaf records in tree */
  299.   ULongInt    bthFNode;    /* number of first leaf node */
  300.   ULongInt    bthLNode;    /* number of last leaf node */
  301.   UInteger    bthNodeSize;    /* size of a node */
  302.   UInteger    bthKeyLen;    /* maximum length of a key */
  303.   ULongInt    bthNNodes;    /* total number of nodes in tree */
  304.   ULongInt    bthFree;    /* number of free nodes */
  305.   SignedByte    bthResv[76];    /* reserved */
  306. } BTHdrRec;
  307.  
  308. typedef struct _btree_ {
  309.   hfsfile f;            /* subset file information */
  310.   node hdrnd;            /* header node */
  311.   BTHdrRec hdr;            /* header record */
  312.   char *map;            /* usage bitmap */
  313.   unsigned long mapsz;        /* number of bytes in bitmap */
  314.   int flags;            /* bit flags */
  315.  
  316.   int (*compare) __PR((unsigned char *, unsigned char *));
  317.                 /* key comparison function */
  318. } btree;
  319.  
  320. # define HFS_UPDATE_BTHDR    0x01
  321.  
  322. struct _hfsvol_ {
  323.   int fd;        /* volume's open file descriptor */
  324.   int flags;        /* bit flags */
  325.  
  326. #ifdef APPLE_HYB
  327.   hce_mem *hce;        /* Extras needed by libhfs/mkisofs */
  328. #endif /* APPLE_HYB */
  329.  
  330.   int pnum;        /* ordinal HFS partition number */
  331.   unsigned long vstart;    /* logical block offset to start of volume */
  332.   unsigned long vlen;    /* number of logical blocks in volume */
  333.   unsigned int lpa;    /* number of logical blocks per allocation block */
  334.  
  335.   MDB mdb;        /* master directory block */
  336.   block *vbm;        /* volume bit map */
  337.   btree ext;        /* B*-tree control block for extents overflow file */
  338.   btree cat;        /* B*-tree control block for catalog file */
  339.   long cwd;        /* directory id of current working directory */
  340.  
  341.   int refs;        /* number of external references to this volume */
  342.   hfsfile *files;    /* list of open files */
  343.   hfsdir *dirs;        /* list of open directories */
  344.  
  345.   struct _hfsvol_ *prev;
  346.   struct _hfsvol_ *next;
  347. };
  348.  
  349. # define HFS_READONLY        0x01
  350.  
  351. # define HFS_UPDATE_MDB        0x10
  352. # define HFS_UPDATE_ALTMDB    0x20
  353. # define HFS_UPDATE_VBM        0x40
  354.  
  355. extern hfsvol *hfs_mounts;
  356. extern hfsvol *hfs_curvol;
  357.